import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const originPush = VueRouter.prototype.push
const originReplace = VueRouter.prototype.replace
// 重写push方法
VueRouter.prototype.push = function push(location, resolve, reject) {
  if (resolve && reject) {
    // 这里不能直接调用originPush（）因为函数名+括号 上下文是windows
    // 这里的this是VueRouter
    // this.router是VueRouter里prototype的一个方法
    return originPush.call(this, location, resolve, reject)
  } else {
    return originPush.call(this, location).catch(err => err)
  }
}

// 重写replace
VueRouter.prototype.replace = function(location, resolve, reject) {
  if (resolve && reject) {
    originReplace.call(this, location, resolve, reject)
  } else {
    originReplace.call(this, location, () => {}, () => {})
  }
}

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/404',
    component: () => import('@/views/404')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login'),
    meta: {
      head: false, footer: false, navbar: false
    }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/Register'),
    meta: {
      head: false, footer: false, navbar: false
    }
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('@/views/Home'),
    meta: {
      head: true, footer: true, navbar: true
    }
  },
  {
    path: '/personal',
    name: 'Personal',
    redirect: '/personal/userinfo',
    component: () => import('@/views/Personal'),
    children: [{
      path: 'userinfo',
      name: 'UserInfo',
      component: () => import('@/views/Personal/UserInfo'),
      meta: {
        head: true, footer: true, navbar: true
      }
    }, {
      path: 'userconsume',
      name: 'UserConsume',
      component: () => import('@/views/Personal/UserConsume'),
      meta: {
        head: true, footer: true, navbar: true
      }
    },
    {
      path: 'studyrecord',
      name: 'StudyRecord',
      component: () => import('@/views/Personal/StudyRecord'),
      meta: {
        head: true, footer: true, navbar: true
      }
    },
    {
      path: 'usercollect',
      name: 'UserCollect',
      component: () => import('@/views/Personal/UserCollect'),
      meta: {
        head: true, footer: true, navbar: true
      }
    }
    ]
  },
  {
    path: '/searchteacher',
    name: 'SearchTeacher',
    component: () => import('@/views/SearchTeacher'),
    meta: {
      head: true, footer: true, navbar: true
    }
  }, {
    path: '/socket',
    name: 'Socket',
    component: () => import('@/views/Socket'),
    meta: {
      head: true, footer: true, navbar: true
    }
  },
  {
    path: '/class',
    name: 'Class',
    redirect: '/class/allclass',
    component: () => import('@/views/Class'),
    children: [
      {
        path: 'allclass',
        name: 'AllClass',
        component: () => import('@/views/Class/AllClass'),
        meta: {
          head: true, footer: true, navbar: true
        }
      },
      {
        path: 'classinfo',
        name: 'ClassInfo',
        component: () => import('@/views/Class/ClassInfo'),
        meta: {
          head: true, footer: true, navbar: true
        }
      },
      {
        path: 'myclass',
        name: 'MyClass',
        component: () => import('@/views/Class/MyClass'),
        meta: {
          head: true, footer: true, navbar: true
        }
      },
      {
        path: 'findteacher',
        name: 'FindTeacher',
        component: () => import('@/views/Class/FindTeacher'),
        meta: {
          head: true, footer: true, navbar: true
        }
      }

    ]
  },
  {
    path: '/teacherinfo/:id',
    name: 'TeacherInfo',
    component: () => import('@/views/TeacherInfo'),
    meta: {
      head: true, footer: true, navbar: true
    }
  },
  // 404 page must be placed at the end !!!必修要加在最后
  { path: '*', redirect: '/404', hidden: true }

]

const createRouter = () => new VueRouter({
  routes,
  scrollBehavior: () => ({ y: 0 })
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router

